﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using BLL;

namespace WebApplication1.Admin
{
    public partial class QLUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillGrid();
            }
        }
        public void FillGrid()
        {
            List<BLLNguoiDung> nd = new List<BLLNguoiDung>();
            nd = new BLLNguoiDung().GetAll;
            GvQLUser.DataSource = nd;
            GvQLUser.DataBind();
        }
        protected void cancel(object sender, GridViewCancelEditEventArgs e)
        {
            GvQLUser.EditIndex = -1;
            FillGrid();
        }

        protected void delete(object sender, GridViewDeleteEventArgs e)
        {
            //BLLNguoiDung nd = new BLLNguoiDung(GvQLUser.DataKeys[e.RowIndex].Values[0].ToString());
            //nd.Delete();
            //FillGrid();
        }

        protected void edit(object sender, GridViewEditEventArgs e)
        {
            GvQLUser.EditIndex = e.NewEditIndex;
            FillGrid();
        }

        protected void update(object sender, GridViewUpdateEventArgs e)
        {
            Label id = (Label)GvQLUser.Rows[e.RowIndex].FindControl("LbID");
            TextBox matkhau =(TextBox)GvQLUser.Rows[e.RowIndex].FindControl("txtmatkhau");
            TextBox phanquyen = (TextBox)GvQLUser.Rows[e.RowIndex].FindControl("txtphanquyen");
            TextBox hoten = (TextBox)GvQLUser.Rows[e.RowIndex].FindControl("txthoten");
            TextBox email  = (TextBox)GvQLUser.Rows[e.RowIndex].FindControl("txtemail");
            TextBox sodienthoai= (TextBox)GvQLUser.Rows[e.RowIndex].FindControl("txtsodienthoai");
            TextBox ghichu= (TextBox)GvQLUser.Rows[e.RowIndex].FindControl("txtghichu");
            CheckBox userxau = (CheckBox)GvQLUser.Rows[e.RowIndex].FindControl("CheckBox1");
            TextBox tienmat= (TextBox)GvQLUser.Rows[e.RowIndex].FindControl("txttienmat");
            //if (matkhau.Text.Trim() == "")
            //{
            //    Lbthongbao.Text = "Mật khẩu không được để trống !";
            //}
            //else if (matkhau.Text.Trim().Length < 6)
            //{
            //    Lbthongbao.Text = "Mật khẩu phải nhiều hơn 6 kí tự !";
            //}
             if (hoten.Text.Trim() == "")
            {
                Lbthongbao.Text = "Họ tên không được để trống!";
            }
            else if (email.Text.Trim() == "")
            {
                Lbthongbao.Text = "Email không được để trống!";
            }
            else if (!IsEmail(email.Text.Trim()))
            {
                Lbthongbao.Text = "Email không hợp lệ!";
            }
            else if (sodienthoai.Text.Trim() == "")
            {
                Lbthongbao.Text = "Số điện thoại không được trống !";
            }
            else if (sodienthoai.Text.Trim().Length < 8 || chekcnum(sodienthoai.Text.Trim()) == false)
            {
                Lbthongbao.Text = "Số điện thoại không hợp lệ !";
            }
            else if (tienmat.Text.Trim() == "" || chekcnum(tienmat.Text.Trim()) == false)
            {
                Lbthongbao.Text = "Tài khoản tiền mặt không hợp lệ !";
            }
            else
            {
                BLLNguoiDung nd = new BLLNguoiDung(id.Text,"", phanquyen.Text, hoten.Text, email.Text, sodienthoai.Text, userxau.Checked, tienmat.Text);
                nd.Update();
                Response.Redirect(Request.RawUrl);
            }
            //FillGrid(); 
        }

        protected void RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                
          
                try
                {
                    //neu xoa Admin
                    string idnd = e.CommandArgument.ToString();
                    if (idnd == "Admin")
                    {
                        Lbthongbao.Text = "Không thể xóa chính Admin!";
                    }
                    else
                    {
                        List<BLLDonHang> dh = new List<BLLDonHang>();
                        dh = new BLLDonHang("", idnd).GetByUser();
                        int i = 0;
                        //xoa chi tiet don hang co trong don hang
                        while (i < dh.Count)
                        {
                            BLLChiTietDonHang ct = new BLLChiTietDonHang(dh[i].Iddonhang);
                            string iddh = ct.DeletebyDonhang();
                            i++;
                        }

                        //xoa don hang cua nguoi dung
                        BLLDonHang donhang = new BLLDonHang("", idnd);
                        string hang = donhang.DeleteByUser();
                        //xoa user 
                        BLLNguoiDung nd = new BLLNguoiDung(idnd);
                        nd.Delete();
                        FillGrid();
                    }
                }
                catch(Exception ex)
                {
                    Lbthongbao.Text = ex.ToString();
                }
            }
        }
        //kiem tra chuoi nhap vao co phai la so ko
        private bool chekcnum(string num)
        {
            decimal number;
            bool isnum = false;
            isnum = decimal.TryParse(num, out number);
            return isnum;
        }
        //kiem tra email hop le
        public const string MatchEmailPattern = @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
+ @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
+ @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
+ @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$";
        public static bool IsEmail(string email)
        {
            if (email != null) return Regex.IsMatch(email, MatchEmailPattern);
            else return false;
        }

        protected void DataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void GvQLUser_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GvQLUser.PageIndex = e.NewPageIndex;
            FillGrid();
        }

    }
}
